package com.huwaiwai001.echo.bio.server;

import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;

/**
 * Created by hww on 2019/3/18 11:31.
 */
public class EchoBioServerTask implements Runnable {

    private Socket socket;

    EchoBioServerTask(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try (Socket client = this.socket) {
            Scanner scan = new Scanner(client.getInputStream());
            PrintStream out = new PrintStream(client.getOutputStream());
            scan.useDelimiter("\n");
            boolean flag = true;
            while (flag) {
                if (scan.hasNext()) {
                    String inputData = scan.next();
                    if ("exit".equalsIgnoreCase(inputData)) {
                        flag = false;
                        out.println("【ECHO】Bye Bye ... ");
                    } else {
                        out.println("【ECHO】" + inputData);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
